package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Mission;
import cn.wolfcode.crm.query.MissionQueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.IMissionService;
import cn.wolfcode.crm.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/mission")
public class MissionController {
    @Autowired
    private IMissionService missionService;
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/view")
    public String view() {
        return "/employee_management/mission";
    }

    @RequestMapping("/query")
    @ResponseBody
    public Object query(MissionQueryObject qo) {

        return missionService.query(qo);
    }

    @RequestMapping("/changeState")
    @ResponseBody
    public JsonResult changeState(Boolean state, Long id) {

        try {
            missionService.changeState(state, id);
        } catch (Exception ex) {
            ex.printStackTrace();
            return new JsonResult().mark("操作失败");
        }
        return new JsonResult();
    }

    @RequestMapping("/validateName")
    @ResponseBody
    public Boolean validateName(String employeeuserame) {

        return missionService.validateName(employeeuserame);
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Mission mission) {
        try {
            if (mission.getId() != null) {
                missionService.updateByPrimaryKey(mission);
            } else {
                Employee employee = employeeService.getByUsername(mission.getEmployeeuserame());
                mission.setDeptname(employee.getDept().getName());
                mission.setState(false);
                missionService.insert(mission);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("保存失败!");
        }

    }

}
